import {Injectable} from '@nestjs/common';
// ORM
import {InjectRepository} from '@nestjs/typeorm';
import {Repository, UpdateResult} from 'typeorm';
import {AppListEntity} from '../entity/app.list.entity';
import {AppListDto} from '../dto/app.list.dto';
import {AppStatus} from "../common/constants";

@Injectable()
export class AppListService {
    constructor(
        @InjectRepository(AppListEntity)
        private readonly appRepository: Repository<AppListEntity>,
    ) {
    }

    async createOne(appListDto: AppListDto): Promise<AppListEntity> {
        return await this.appRepository.save(appListDto);
    }

    async getOneByAppId(appId: string): Promise<AppListEntity> {
        const qb = this.appRepository.createQueryBuilder();
        qb.where({appId})
        return await qb.getOne()
    }

    async editStatus(appId: string, status: AppStatus): Promise<UpdateResult> {
        return await this.appRepository.update({appId}, {status});
    }

    async getAllAppList(): Promise<AppListEntity[]> {
        const qb = this.appRepository.createQueryBuilder();
        return await qb.getMany();
    }
}
